<template>
  <div class="shopCart">
    <div class="goods-list" v-for="(item,i) in goodsList" :key="item.id">
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getSelStatus[item.id]"
              @change="choose(item.id,$store.getters.getSelStatus[item.id])"
            ></mt-switch>
            <img :src="item.url" alt />
            <div class="info">
              <h1>{{item.title}}</h1>
              <p class="info-content">
                <span class="infoPrice">￥{{item.newPrice}}</span>
                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsId="item.id"></numbox>
                <a href="javascript:;" @click.prevent="removeCart(item.id,i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计（不含运费）</p>
            <p>
              已购选商品
              <span class="red">{{$store.getters.getMount.amount}}</span> 件，总价：
              <span class="red">￥{{$store.getters.getMount.price}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.shopCart {
  background: #eee;
  .mui-card-content-inner {
    display: flex;
    align-items: center;
  }
  img {
    width: 60px;
    height: 60px;
  }
  .info {
    h1 {
      font-size: 16px;
      margin-bottom: 8px;
    }
    .infoPrice {
      color: red;
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    .red {
      color: red;
      font-weight: 700;
      font-size: 16px;
    }
  }
}
</style>

<script>
import numbox from "../subcomponents/shopCarNumbox.vue";
export default {
  data() {
    return {
      goodsList: []
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      //取出id
      var arr = [];
      this.$store.state.car.forEach(item => {
        arr.push(item.id);
      });
      //改造数据
      var arr = arr.map(item => "id=" + item);
      if (arr.length <= 0) {
        return;
      }
      this.$http.get("goodsList?" + arr.join("&")).then(res => {
        this.goodsList = res.body;
      });
    },
    removeCart(id, i) {
      this.goodsList.splice(i, 1);
      this.$store.commit("removeCart", id);
    },
    choose(id, val) {
      this.$store.commit("updateSelStatue", { id, sel: val });
    }
  },
  components: {
    numbox
  }
};
</script>